/*!
 * Copyright (C) 2001-2008 by egnite Software GmbH. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the copyright holders nor the names of
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * For additional information see http://www.ethernut.de/
 */

/*!
 * $Log: displayd.c,v $
 *
 */

/*!
 * \example displayd/displayd.c
 *
 * TCP server for LCD Display.
 *
 * Program your board with displayd.hex and enter
 *
 * telnet x.x.x.x 12345
 *
 * two times on two command prompts, replacing x.x.x.x with the IP address 
 * of your ethernut board. This will start two telnet session. 
 *
 * Enter help for a list of available commands.
 *
 * Enter query on the first will show the current port status.
 *
 * Then enter wait on this session, which will hang until the port status 
 * changes. 
 *
 * On the second telnet session enter set1 to set the first output bit.
 */

#define MY_MAC          {0x00,0x06,0x98,0x20,0x00,0x00}
#define MY_IP           "192.168.0.100"
#define MY_MASK         "255.255.255.0"
#define MY_PORT         12345

#include <string.h>
#include <stdio.h>

#include <dev/board.h>
#include <dev/gpio.h>
#include <dev/term.h>

#include <sys/heap.h>
#include <sys/thread.h>
#include <sys/timer.h>
#include <sys/socket.h>

#include <arpa/inet.h>
#include <net/route.h>
#include <netdb.h>

#include <pro/dhcp.h>

#include <avr32/io.h>

void service(FILE* lcd)
{
    TCPSOCKET *sock;

    /*
     * Loop endless for connections.
     */
    for (;;) {
		unsigned char token;
        /*
         * Create a socket.
         */
        sock = NutTcpCreateSocket();

        /*
         * Listen at the configured port. If we return, we got a client.
         */
        NutTcpAccept(sock, MY_PORT);

		for (;;) {
			if (NutTcpReceive( sock, &token, 1 ) <= 0)
				break;
		
			fprintf( lcd, "%c", token );
		}

        /*
         * Close our socket.
         */
        NutTcpCloseSocket(sock);
    }
}

THREAD(service_thread, arg)
{
	FILE* lcd = (FILE*) arg;
    for (;;)
        service(lcd);
}

extern NUTDEVICE devSpiDip204;

/*
 * Main application routine. 
 *
 * Nut/OS automatically calls this entry after initialization.
 */
int main(void)
{
	FILE *lcd;
    u_char my_mac[] = MY_MAC;

    /*
     * Initialize display.
     */
    NutRegisterSpiDevice(&devSpiDip204, &DEV_SPIBUS1, 2);
	NutRegisterDevice(&devSpiDip204, 0, 0);

	lcd = fopen("lcd", "w");

    /*
     * Register the ethernet controller. For Ethernut 1, this is at address 8300 hex
     * and interrupt 5.
     */
    NutRegisterDevice(&DEV_ETHER, AVR32_MACB_ADDRESS, 0);

    /*
     * Configure lan interface. 
     */
    if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000) && NutDhcpIfConfig("eth0", my_mac, 60000)) {
        /*
         * No DHCP server available. Use hard coded values.
         */
        u_long ip_addr = inet_addr(MY_IP);      /* ICCAVR fix. */
        NutNetIfConfig("eth0", my_mac, ip_addr, inet_addr(MY_MASK));
    }

    /*
     * Start another service thread to allow
     * two concurrent connections.
     */
    NutThreadCreate("sback", service_thread, lcd, 1384);

    for (;;)
        service(lcd);

    return 0;
}
